package com.cxf.windex.rest;

import com.cxf.windex.base.msg.ObjectRestResponse;
import com.cxf.windex.base.msg.TableResultResponse;
import com.cxf.windex.base.rest.BaseRestController;
import com.cxf.windex.base.util.IdUtil;
import com.cxf.windex.biz.JsFollowBiz;
import com.cxf.windex.biz.JsTechBiz;
import com.cxf.windex.entity.Address;
import com.cxf.windex.entity.JsFollow;
import com.cxf.windex.entity.JsTech;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author chenxf
 * @date 3/20/2018
 * @description
 */
@Api(tags = {"小程序 - 关注接口"})
@Controller
@RequestMapping("/user/follow")
public class FollowController extends BaseRestController<JsFollowBiz, JsFollow> {

  @Autowired
  private JsTechBiz jsTechBiz;

  @ApiOperation(value = "查询关注技师列表", notes = "查询关注技师列表")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "limit", value = "记录数", required = false, paramType = "query", dataType = "int"),
      @ApiImplicitParam(name = "offset", value = "页码", required = false, paramType = "query", dataType = "int"),
      @ApiImplicitParam(name = "latitude", value = "中心点经度", required = false, paramType = "query", dataType = "String"),
      @ApiImplicitParam(name = "longitude", value = "中心点纬度", required = false, paramType = "query", dataType = "String"),
  })
  @RequestMapping(value = "/list", method = RequestMethod.GET)
  @ResponseBody
  public TableResultResponse<Address> list(@RequestParam(defaultValue = "10") int limit,
                                           @RequestParam(defaultValue = "1") int offset,
                                           String latitude, String longitude) {
    String userId = this.getCurrentUserId();

    if ( userId==null || userId.equals("") ) {
      //return new TableResultResponse<Address>(0, offset, limit,new ArrayList<Address>());
      userId="1";
    }
    PageHelper.startPage(offset, limit);

    List<Address> items = baseBiz.selectList(latitude,longitude,userId);

    PageInfo<Address> pageInfo = new PageInfo<Address>(items);
    return new TableResultResponse<Address>(Integer.parseInt(String.valueOf(pageInfo.getTotal())),
        offset, limit,
        items);
  }

  @ApiOperation(value = "关注", notes = "关注接口")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "jsId", value = "受赞技师", required = false, paramType = "query", dataType = "String"),
      @ApiImplicitParam(name = "type", value = "0取消 1关注", required = false, paramType = "query", dataType = "int")
  })
  @RequestMapping(value = "/like", method = RequestMethod.GET)
  @ResponseBody
  public ObjectRestResponse edit(String jsId, @RequestParam(defaultValue = "1") int type) {
    if (StringUtils.isBlank(jsId)) {
      return new ObjectRestResponse().rel(false).msg("请选择点赞技师");
    }
    JsTech entity = new JsTech();
    entity.setId(jsId);
    JsTech tech = jsTechBiz.selectOne(entity);
    if (tech == null) {
      return new ObjectRestResponse().rel(false).msg("找不到ID为" + jsId + "的技师");
    }
    String userId = this.getCurrentUserId();
    if (userId==null) {
      //return new ObjectRestResponse().rel(false).msg("无法获取用户信息");
      userId="1";
    }
    if (userId.equals("" + tech.getUserid())) {
      return new ObjectRestResponse().rel(false).msg("不能关注自己");
    }
    JsFollow queryEntity = new JsFollow();
    queryEntity.setUserid(userId);
    queryEntity.setTouserid(jsId);
    JsFollow follow = baseBiz.selectOne(queryEntity);
    if (follow == null && type == 0) {
      return new ObjectRestResponse().rel(false).msg("此技师不在你的关注列表，无需取消关注");
    }
    if (follow != null && type == 1) {
      return new ObjectRestResponse().rel(false).msg("此技师已经在你的关注列表");
    }
    addFollows(tech, type);
    if (type == 0) {
      baseBiz.deleteById(follow.getId());
      return new ObjectRestResponse().rel(true).msg("取消关注成功");
    }
    queryEntity.setId(new IdUtil(4, 1).nextId() + "");
    baseBiz.insert(queryEntity);
    return new ObjectRestResponse().rel(true).msg("关注成功");
  }

  private void addFollows(JsTech tech, int type) {
    JsTech entity = new JsTech();
    entity.setId(tech.getId());
    if (type == 0) {
      entity.setFollowers(tech.getFollowers() - 1);
    } else {
      entity.setFollowers(tech.getFollowers() + 1);
    }
    jsTechBiz.updateSelectiveById(entity);
  }
}
